/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UserDetailsPanel.java
 *
 * Created on 2011-03-14, 03:58:07
 */
package guzik.lukasz.UsersExplorer;

import SnpDbEntities.Category;
import SnpDbEntities.User;
import SnpDbEntities.UserPermissions;
import guzik.lukasz.SnpDataAPI.SnpDataAPI;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import javax.swing.DefaultListModel;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;

/**
 *
 * @author Łukasz 'byku' Guzik
 */
public class UserDetailsPanel extends javax.swing.JPanel
{
    private boolean isEditable;
    private User user;
    String pwdHash = "0";
    ArrayList<Integer> addToModerate = new ArrayList<Integer>();
    ArrayList<Integer> removedFromModerate = new ArrayList<Integer>();

    /** Creates new form UserDetailsPanel */
    public UserDetailsPanel(User user, boolean isEditable)
    {
	initComponents();
	this.isEditable = isEditable;
	this.user = user;

	initPermissionsComboBox();
	initModeratorsList();
	setEditable();
	setButtonsEnabled(false);

	jLoginTextField.setText(user.getLogin());
	jNameTextField.setText(user.getName());
	jDescriptionTextArea.setText(user.getAbout());
	jEmailTextField.setText(user.getEmail());
	jUserEnabledCheckbox.setSelected(user.isEnabled());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLoginTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jNameTextField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jEmailTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jPermissionsComboBox = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jDescriptionTextArea = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jModCategories = new javax.swing.JList();
        jAddCatButton = new javax.swing.JButton();
        jDelCatButton = new javax.swing.JButton();
        jUserEnabledCheckbox = new javax.swing.JCheckBox();
        jPwdChangeButton = new javax.swing.JButton();

        jLabel1.setText(org.openide.util.NbBundle.getMessage(UserDetailsPanel.class, "UserDetailsPanel.jLabel1.text")); // NOI18N

        jLoginTextField.setText(org.openide.util.NbBundle.getMessage(UserDetailsPanel.class, "UserDetailsPanel.jLoginTextField.text")); // NOI18N

        jLabel2.setText(org.openide.util.NbBundle.getMessage(UserDetailsPanel.class, "UserDetailsPanel.jLabel2.text")); // NOI18N

        jNameTextField.setText(org.openide.util.NbBundle.getMessage(UserDetailsPanel.class, "UserDetailsPanel.jNameTextField.text")); // NOI18N

        jLabel3.setText(org.openide.util.NbBundle.getMessage(UserDetailsPanel.class, "UserDetailsPanel.jLabel3.text")); // NOI18N

        jEmailTextField.setText(org.openide.util.NbBundle.getMessage(UserDetailsPanel.class, "UserDetailsPanel.jEmailTextField.text")); // NOI18N

        jLabel4.setText(org.openide.util.NbBundle.getMessage(UserDetailsPanel.class, "UserDetailsPanel.jLabel4.text")); // NOI18N

        jPermissionsComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jDescriptionTextArea.setColumns(20);
        jDescriptionTextArea.setRows(5);
        jScrollPane1.setViewportView(jDescriptionTextArea);

        jLabel5.setText(org.openide.util.NbBundle.getMessage(UserDetailsPanel.class, "UserDetailsPanel.jLabel5.text")); // NOI18N

        jLabel6.setText(org.openide.util.NbBundle.getMessage(UserDetailsPanel.class, "UserDetailsPanel.jLabel6.text")); // NOI18N

        jModCategories.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(jModCategories);

        jAddCatButton.setText(org.openide.util.NbBundle.getMessage(UserDetailsPanel.class, "UserDetailsPanel.jAddCatButton.text")); // NOI18N

        jDelCatButton.setText(org.openide.util.NbBundle.getMessage(UserDetailsPanel.class, "UserDetailsPanel.jDelCatButton.text")); // NOI18N

        jUserEnabledCheckbox.setText(org.openide.util.NbBundle.getMessage(UserDetailsPanel.class, "UserDetailsPanel.jUserEnabledCheckbox.text")); // NOI18N

        jPwdChangeButton.setText(org.openide.util.NbBundle.getMessage(UserDetailsPanel.class, "UserDetailsPanel.jPwdChangeButton.text")); // NOI18N
        jPwdChangeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPwdChangeButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jUserEnabledCheckbox)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 68, Short.MAX_VALUE)
                        .addComponent(jPwdChangeButton))
                    .addComponent(jLabel6)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addGap(9, 9, 9)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jNameTextField)
                                    .addComponent(jLoginTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(31, 31, 31)
                                        .addComponent(jLabel3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                                        .addComponent(jEmailTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jPermissionsComboBox, 0, 144, Short.MAX_VALUE))))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jDelCatButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jAddCatButton))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jUserEnabledCheckbox)
                    .addComponent(jPwdChangeButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLoginTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(jEmailTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jPermissionsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jAddCatButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jDelCatButton))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jPwdChangeButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jPwdChangeButtonActionPerformed
    {//GEN-HEADEREND:event_jPwdChangeButtonActionPerformed
	PasswordChangePanel pcd = new PasswordChangePanel();
	DialogDescriptor dd = new DialogDescriptor(pcd, "Zmiana hasła",	true, null);
	dd.setClosingOptions(new Object[]
		{
		    DialogDescriptor.OK_OPTION, DialogDescriptor.CANCEL_OPTION
		});
	dd.setOptions(new Object[]
		{
		    DialogDescriptor.OK_OPTION, DialogDescriptor.CANCEL_OPTION
		});
	if(DialogDisplayer.getDefault().notify(dd) == DialogDescriptor.OK_OPTION)
	{
	    pwdHash = User.calculateSHA1(pcd.newPwd);
	}
    }//GEN-LAST:event_jPwdChangeButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jAddCatButton;
    private javax.swing.JButton jDelCatButton;
    javax.swing.JTextArea jDescriptionTextArea;
    javax.swing.JTextField jEmailTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    javax.swing.JTextField jLoginTextField;
    private javax.swing.JList jModCategories;
    javax.swing.JTextField jNameTextField;
    javax.swing.JComboBox jPermissionsComboBox;
    private javax.swing.JButton jPwdChangeButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    javax.swing.JCheckBox jUserEnabledCheckbox;
    // End of variables declaration//GEN-END:variables

    private void initPermissionsComboBox()
    {
	jPermissionsComboBox.removeAllItems();
	jPermissionsComboBox.addItem("Super administrator");
	jPermissionsComboBox.addItem("Administrator");
	jPermissionsComboBox.addItem("Moderator");
	jPermissionsComboBox.addItem("Zwykły użytkownik");

	switch(user.getPermissions())
	{
	    case SUPER_ADMIN:
		jPermissionsComboBox.setSelectedIndex(0);
		break;

	    case ADMIN:
		jPermissionsComboBox.setSelectedIndex(1);
		break;

	    case MODERATOR:
		jPermissionsComboBox.setSelectedIndex(2);
		break;

	    case NORMAL_USER:
		jPermissionsComboBox.setSelectedIndex(3);
		break;
	}
    }

    private void setEditable()
    {
	User connectedUser = SnpDataAPI.getDefault().getConnectedUser();
	boolean self = user.getId() == connectedUser.getId();

	jLoginTextField.setEditable(false);
	jNameTextField.setEditable(false);
	jEmailTextField.setEditable(false);
	jDescriptionTextArea.setEditable(false);
	jPermissionsComboBox.setEnabled(false);
	jModCategories.setEnabled(false);
	jUserEnabledCheckbox.setEnabled(false);
	jPwdChangeButton.setEnabled(false);

	if(!isEditable)
	{
	    return;
	}

	if(self)
	{
	    jNameTextField.setEditable(true);
	    jEmailTextField.setEditable(true);
	    jDescriptionTextArea.setEditable(true);
	    jPwdChangeButton.setEnabled(true);
	}

	if(connectedUser.getPermissions() == UserPermissions.MODERATOR
		|| connectedUser.getPermissions() == UserPermissions.NORMAL_USER)
	{
	    return;
	}

	if(connectedUser.getPermissions() == UserPermissions.SUPER_ADMIN)
	{
	    jLoginTextField.setEditable(true);
	    jNameTextField.setEditable(true);
	    jEmailTextField.setEditable(true);
	    jDescriptionTextArea.setEditable(true);
	    jPermissionsComboBox.setEnabled(true);
	    jUserEnabledCheckbox.setEnabled(true);
	    jPwdChangeButton.setEnabled(true);
	}

	if(connectedUser.getPermissions() == UserPermissions.ADMIN)
	{
	    if(user.getPermissions() == UserPermissions.SUPER_ADMIN)
	    {
		return;
	    }

	    if(!self && user.getPermissions() == UserPermissions.ADMIN)
	    {
		return;
	    }

	    jLoginTextField.setEditable(true);
	    jNameTextField.setEditable(true);
	    jEmailTextField.setEditable(true);
	    jDescriptionTextArea.setEditable(true);
	    jPermissionsComboBox.setEnabled(true);
	    jUserEnabledCheckbox.setEnabled(true);
	    jPwdChangeButton.setEnabled(true);
	}

	if(user.getPermissions() == UserPermissions.MODERATOR)
	{
	    jModCategories.setEnabled(true);
	} else
	{
	    jModCategories.setEnabled(false);
	}
    }

    private void initModeratorsList()
    {
	DefaultListModel model = new DefaultListModel();
	jModCategories.setModel(model);

	if(user.getPermissions() != UserPermissions.MODERATOR)
	{
	    return;
	}

	LinkedHashMap<Integer, Category> categories = SnpDataAPI.getDefault().getAllPublicCategories();
	ArrayList<Integer> mods;

	for(Category cat : categories.values())
	{
	    mods = SnpDataAPI.getDefault().getCategoryModerators(cat.getId());
	    if(mods.contains(user.getId()))
	    {
		model.addElement(cat);
	    }
	}
    }

    private void setButtonsEnabled(boolean enabled)
    {
	jAddCatButton.setEnabled(enabled);
	jDelCatButton.setEnabled(enabled);
    }
}
